package com.qianfeng.smartplatform.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import javax.sql.DataSource;

/**
 * Created by Jackiechan on 2022/9/1 14:31
 * 这是一个配置类,用来代替xml文件件的
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Configuration//声明当前类是一个配置类,相当于代替了xml中的<beans>标签
@EnableTransactionManagement
@EnableAsync(proxyTargetClass = true)
@EnableWebSocket
public class BeanConfig {

    // <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
    //代替了<bean>标签, 返回值的类型就是bean标签内的class,默认情况下,方法的名字就是id
    //如果想要改变id,可以在注解内声明新的id
   // @Bean("xindeid")
    @Bean
    public DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///sys_system?useUnicode=true&characterEncoding=utf8");
        return dataSource;
    }

    /**
     * 如果有参数,spring会查找自己管理的对象中有没有这个类型的对象,如果有就注入过来,如果有多个,形参的名字需要和想要引用 对象的id保持一致
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置分页插件
        sqlSessionFactoryBean.setPlugins(new PageInterceptor());
        //创建配置类
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        //设置驼峰
        configuration.setMapUnderscoreToCamelCase(true);
        //设置日志
        configuration.setLogImpl(StdOutImpl.class);
        return sqlSessionFactoryBean;
    }

    /**
     * 扫描mapper
     * @return
     */
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer configurer = new MapperScannerConfigurer();
        configurer.setBasePackage("com.qianfeng.smartplatform.mapper");
        return configurer;
    }

    /**
     * 平台事务管理器
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(15);
        executor.setMaxPoolSize(20);
        executor.setThreadNamePrefix("bk2205-");
        return executor;
    }


    @Bean
    public ObjectMapper objectMapper() {
        return new ObjectMapper();
    }
}
